package models

import (
	"fmt"
	"proxy_resource_test/dao"
	"proxy_resource_test/utils/logging"
	"time"
)

type TaskConfig struct {
	Id           int64  `gorm:"primary_key" json:"-"`
	Operate      string `json:"operate" comment:"测试的标识" form:"operate" binding:"required" gorm:"column:operate;type:varchar(255);comment:测试的标识"`
	Total        int32  `json:"total" form:"total" gorm:"total"` // 0 表示不限制
	Duration     int64  `json:"duration" form:"duration" gorm:"column:duration;type:int(11);comment:持续时间(小时)"`
	Concurrency  int    `json:"concurrency" form:"concurrency" gorm:"column:concurrency;type:int(11);comment:并发数"`
	TargetURL    string `json:"target_url" form:"target_url" binding:"required" gorm:"column:target_url;type:varchar(255);comment:目标地址"`
	UserTpl      string `json:"user_tpl" form:"user_tpl" binding:"required" gorm:"column:user_tpl;type:varchar(255);comment:用户名模板"`
	User         string `json:"user" form:"user" binding:"required" gorm:"column:user;type:varchar(255);comment:用户名"`
	Password     string `json:"password" form:"password" binding:"required" gorm:"column:password;type:varchar(255);comment:密码"`
	ProxyAddr    string `json:"proxy_addr" form:"proxy_addr" binding:"required" gorm:"column:proxy_addr;type:varchar(255);comment:代理地址"`
	CreatedAt    int64  `json:"-" gorm:"column:created_at;type:int(20);comment:创建时间"`
	Popular      string `json:"popular" form:"popular"  gorm:"column:popular;varchar(255);comment:是否生成热门"`
	MachineSnMin int    `json:"machine_no_min" form:"machine_no_min" gorm:"column:machine_no_min;comment:机器编号起"`
	MachineSnMax int    `json:"machine_no_max" form:"machine_no_max" gorm:"column:machine_no_max;comment:机器编号止"`
	OperateName  string `json:"operate_name" form:"operate_name" gorm:"column:operate_name;comment:任务三方名称"`
}

func (t *TaskConfig) TableName() string {
	return "wt_task_schedule"
}

func (t *TaskConfig) SaveSchedule() error {
	t.CreatedAt = time.Now().Unix()
	err := dao.GetSqlDB().Create(t).Error
	return err
}

// AutoMigrateInfo 自动建表
func (t *TaskConfig) AutoMigrateInfo() {
	if !dao.GetSqlDB().Migrator().HasTable(t.TableName()) {
		if err := dao.GetSqlDB().Table(t.TableName()).AutoMigrate(&TaskConfig{}); err != nil {
			logging.Error(fmt.Sprintf("AutoMigrateInfo error: %v", err))
		}
	}
}
